/**
 * 
 */
package com.cait.dutyfree.security;

/**
 * @author Rasika Kaluwalgoda
 *
 */

import java.util.StringTokenizer;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

public class Validator {
	
    public static boolean isValidTelNo(String strDN){
		// try{
		// @SuppressWarnings("unused")
		// long mobno = Long.parseLong(strDN);
		// }catch(NumberFormatException e){
		// return false;
		// }
        
        if(strDN.length()==11 && strDN.startsWith("94")) return true;

        if(strDN.length()==10 && strDN.startsWith("0")) return true;

        if(strDN.length()==9 && ! ( strDN.startsWith("0")) ) return true;
        
        return false;
    }

    public static boolean isValidEmailAddress(String emailAddress){
    	
    	// a null string is invalid
    	if ( emailAddress == null )
    		return false;

    	// a string without a "@" is an invalid email address
    	if ( emailAddress.indexOf("@") < 0 )
    		return false;

    	// a string without a "."  is an invalid email address
    	if ( emailAddress.indexOf(".") < 0 )
    		return false;

    	// a string less than two chars in last email field is an invalid email address
    	if ( lastEmailFieldTwoCharsOrMore(emailAddress) == false )
    		return false;

    	try{
    		@SuppressWarnings("unused")
			InternetAddress internetAddress = new InternetAddress(emailAddress);
    		return true;
    	}catch (AddressException ae){
    		return false;
    	}
    }

    public static boolean isValidWebURL(String webUrl){
    	
    	// a null string is invalid
    	if ( webUrl == null )
    		return false;

    	// if starts with "http" it should be either "http://" or "https://" else is an invalid web url
    	if(webUrl.startsWith("http"))
    		if(! (webUrl.startsWith("http://") || webUrl.startsWith("https://")) )
    			return false;
    		
    	// a string without a "."  is an invalid web url
    	if ( webUrl.indexOf(".") < 0 )
    		return false;

    	// a string less than two chars in last email field is an invalid web url
    	if ( lastEmailFieldTwoCharsOrMore(webUrl) == false )
    		return false;

    	return true;
    }

    private static boolean lastEmailFieldTwoCharsOrMore(String emailAddress){
  	
    	if (emailAddress == null) return false;
    	StringTokenizer st = new StringTokenizer(emailAddress,".");
    	String lastToken = null;
    	while ( st.hasMoreTokens() ){
    		lastToken = st.nextToken();
    	}

    	if ( lastToken.length() >= 2 ){
    		return true;
    	}else{
    		return false;
    	}
    }

    public static boolean isSpecialCharactersinString(String strString) {
		String strValidChars = "*|!\":<>[]{};()@&$#%";
		boolean blnResult = true;
		if (strString.length() == 0)
			return false;
		int i = 0;
		for (i = 0; (i < strString.length()) && (blnResult == true); ++i) {
			char strChar = strString.charAt(i);
			if (strValidChars.indexOf(strChar) == -1)
				continue;
			blnResult = false;
		}
		return !blnResult;
	}

	public static boolean isNumberInString(String strString) {
		String strValidChars = "1234567890";
		boolean blnResult = true;
		if (strString.length() == 0)
			return false;
		int i = 0;
		for (i = 0; (i < strString.length()) && (blnResult == true); ++i) {
			char strChar = strString.charAt(i);
			if (strValidChars.indexOf(strChar) == -1)
				continue;
			blnResult = false;
		}
		return !blnResult;
	}
	
	public static boolean isValidPassword(String username,String password, String cPassword){
		
		if(password.trim().length() < 8) return false;
		if(!isNumberInString(password)) return false;
		if(password.trim().contains(username)) return false;
		if(!isSpecialCharactersinString(password)) return false;
		if(!password.trim().equals(cPassword.trim())) return false;
		
		return true;
	}
	
}
